package part11singleton;

/**
 * @description: 懒汉模式
 * 问题：并发场景下，单例模式失效
 * @author: zhang hang
 * @date: 2024/3/24 14:05.
 * @version: 1.0
 */
public class SingletonTest02 {

    private SingletonTest02() {
        System.out.println(Thread.currentThread().getName() + "starting");
    }

    private static SingletonTest02 instance;


    public static SingletonTest02 getInstance() {
        if (instance == null) {
            instance = new SingletonTest02();
        }
        return instance;
    }

    // 并发场景下，单例模式失效
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                SingletonTest02.getInstance();
            }).start();
        }
    }

}
